package com.company.cloud.club.modules.utils;

import com.company.cloud.club.modules.core.util.ValidateUtil;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.HttpContextUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

public class LoginContextUtils {

    public static Integer getUserId() throws BusinessException {
        HttpServletRequest request = HttpContextUtils.getRequest();
        String val = request.getHeader("userId");
        if (StringUtils.isEmpty(val) || "0".equals(val)) {
            throw new BusinessException(BaseReturnEnum.NOT_LOGIN);
        } else {
            return Integer.valueOf(val);
        }
    }
    public static String getToken() throws BusinessException {
        HttpServletRequest request = HttpContextUtils.getRequest();
        String val = request.getHeader("token");
        if (StringUtils.isEmpty(val)) {
            throw new BusinessException("token不存在或已失效");
        }
        
        return val;
    }

    public static Integer getUserIdOrNotLogin() {
        HttpServletRequest request = HttpContextUtils.getRequest();
        String val = request.getHeader("userId");
        if (ValidateUtil.isValid(val) && !"undefined".equals(val) && !"null".equals(val)) {
            return Integer.valueOf(val);
        } else {
            return 0;
        }
    }
}
